iT邦幫忙

2021 iThome 鐵人賽

DAY 15
0
IT管理

Rocky Linux 8:系統管理與維運實戰系列 第 15

第12章:SSH遠端連線設定與原理介紹(一)

  • 分享至 

  • xImage
  •  

前言

本章節,要講的是SSH遠端連線的機制與原理,以及SSH的使用方式。

什麼是OpenSSH?

OpenSSH實做了Secure Shell或是稱作SSH Protocol,它提供了一種加解密之安全連線方式供遠端的使用者可以透過此加密通訊協定進行遠端連線與存取遠端的主機。以下是一些常見的SSH指令的用法:

[rockylinux@workstation ~]$ ssh rockylinux@localhost
rockylinux@localhost's password:
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Thu Sep 30 23:36:11 2021
[rockylinux@workstation ~]$

從上述的執行指令可以得知,這就是一個基本的SSH指令用法,遠端連上去之後,基本上與在桌面環境上使用終端機的操作一模一樣,當操作完成之後,要離開並關閉這個連線,執行exit或是logout之指令即可,相關的指令執行後所輸出的訊息如下:

[rockylinux@workstation ~]$ ssh rockylinux@localhost
rockylinux@localhost's password:
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Fri Oct  1 00:58:54 2021 from 192.168.0.9
[rockylinux@workstation ~]$ logout
Connection to localhost closed.
[rockylinux@workstation ~]$ ssh rockylinux@localhost
rockylinux@localhost's password:
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Fri Oct  1 00:58:57 2021 from ::1
[rockylinux@workstation ~]$ exit
logout
Connection to localhost closed.
[rockylinux@workstation ~]$

識別遠端使用者

還記得前面的章節有提到w這個指令嗎?這個指令可以查看目前有多少的使用者使用SSH來登入到此主機上,相關的指令執行所輸出的結果如下:

[rockylinux@workstation ~]$ w
 01:01:18 up  3:14,  2 users,  load average: 0.01, 0.00, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
rockylin pts/0    192.168.0.9      00:58    2.00s  0.11s  0.02s w
rockylin tty2     tty2             23:36    3:13m 40.23s  0.27s /usr/libexec/tracker-miner-fs
[rockylinux@workstation ~]$

SSH主機金鑰

當作業系統安裝好之後,主機金鑰便會自己產生,並存放到/etc/ssh/之目錄檔裡面,而這個資料夾也是存放SSH server之設定檔,在這個目錄底下有sshd_config便是可以調整與設定SSH server了,若要觀看更多有關於此設定檔的設定內容說明,可以使用sudo man /etc/ssh/sshd_config指令來做到。

SSH已知主機金鑰管理

當使用SSH第一次連到遠端的主機的時候,會詢問公開金鑰的fingerprint是否要信任遠方主機傳過來的公開金鑰,需先確認主機的公鑰,當輸入「yes」的時候,就會將此公開金鑰存到當前使用者家目錄中的.ssh目錄中,並儲存成known_hosts檔案,接著自此之後再用此指令連上遠端的時候,便不再詢問並直接連上,而當known_hosts所存放的fingerprint有所不同時,在使用SSH進行連線的時候,則會跳出警告,則表示hash值驗證有誤,若確定這個是正常的行為,像是:主機重灌就有可能會發生,因為主機的host key已經重新產一組,則移除host上之fingerprint,並重新建立與儲存host之公鑰之雜湊資訊到known_hosts中,相關的指令執行所輸出的訊息如下:

[rockylinux@workstation ~]$ ssh rockylinux@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:N6ZipKZheNYa53L7olZj/hgn1dmz4POF3HzQuTvgQjI.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

從上面可以發現到,這是第一次連線到rockylinux@127.0.0.1之主機,因此跳出這樣的提示的訊息詢問,這個可以知道是使用ECDSA之Host public key的,因此回到此虛擬主機並使用下列的指令將fingerprint給輸出出來:

[rockylinux@workstation ~]$ ssh rockylinux@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:N6ZipKZheNYa53L7olZj/hgn1dmz4POF3HzQuTvgQjI.
Are you sure you want to continue connecting (yes/no/[fingerprint])? ^C
[rockylinux@workstation ~]$ ssh-keygen -lf /etc/ssh/ssh_host_ecdsa_key.pub
256 SHA256:N6ZipKZheNYa53L7olZj/hgn1dmz4POF3HzQuTvgQjI no comment (ECDSA)
[rockylinux@workstation ~]$

從上述的指令,這就可以發現fingerprint是相同的,接著就可以放心地按下「yes」並將此訊息存放到known_hosts的檔案中,相關的執行指令的操作如下:

[rockylinux@workstation ~]$ ssh rockylinux@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:N6ZipKZheNYa53L7olZj/hgn1dmz4POF3HzQuTvgQjI.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
rockylinux@127.0.0.1's password:
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Fri Oct  1 00:59:07 2021 from ::1
[rockylinux@workstation ~]$ exit
logout
Connection to 127.0.0.1 closed.
[rockylinux@workstation ~]$ cat ~/.ssh/known_hosts
127.0.0.1 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBKnKNWcTQ7yTDdfdZ3rcg7Pg1H+an8BmXZxuyK5CP/BVE/qFVq5Q7EeA2e8M5Qhnw7RS34VdRIVhmt5y3dHqc2o=
[rockylinux@workstation ~]$

接著使用exit指令離開此遠端shell,為了要模擬遠端主機所存取的Host public key有問題,以root使用者並用手動的方式將ssh_host_ecdsa_keyssh_host_ecdsa_key.pub並將sshd服務重新啟動,這時候就會重新產生一組全新的ssh_host_ecdsa_keyssh_host_ecdsa_key.pub檔案了,相關的指令執行輸出的訊息如下:

[rockylinux@workstation ~]$ sudo rm /etc/ssh/ssh_host_ecdsa_key*
[rockylinux@workstation ~]$ ls /etc/ssh/
moduli      ssh_config.d  ssh_host_ed25519_key      ssh_host_rsa_key
ssh_config  sshd_config   ssh_host_ed25519_key.pub  ssh_host_rsa_key.pub
[rockylinux@workstation ~]$ sudo systemctl restart sshd
[rockylinux@workstation ~]$ ls /etc/ssh/
moduli      ssh_config.d  ssh_host_ecdsa_key      ssh_host_ed25519_key      ssh_host_rsa_key
ssh_config  sshd_config   ssh_host_ecdsa_key.pub  ssh_host_ed25519_key.pub  ssh_host_rsa_key.pub
[rockylinux@workstation ~]$

接著再使用ssh rockylinux@127.0.0.1指令進行遠端登入,則會發生fingerprint有問題的錯誤了,相關執行指令的輸出訊息如下:

[rockylinux@workstation ~]$ ssh rockylinux@127.0.0.1
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:jtNpnVUOQFBlVo5shPfHdnogsQq/LtxbFLmgaeJJDjI.
Please contact your system administrator.
Add correct host key in /home/rockylinux/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/rockylinux/.ssh/known_hosts:1
ECDSA host key for 127.0.0.1 has changed and you have requested strict checking.
Host key verification failed.
[rockylinux@workstation ~]$

從上述的警告訊息得知,存放在known_hosts對應到此主機的fingerprint hash與遠端的不同,確認過遠端主機是刪除相關的公私鑰並重新啟動SSH server服務重新產一組的原因之後,可以將此行刪除,接著再次執行ssh rockylinux@127.0.0.1指令,則會詢問fingerprint是不是對的問題了,那就重複上述的步驟進行驗證與登入,相關的指令執行所輸出的訊息如下:

[rockylinux@workstation ~]$ cat ~/.ssh/known_hosts
127.0.0.1 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBKnKNWcTQ7yTDdfdZ3rcg7Pg1H+an8BmXZxuyK5CP/BVE/qFVq5Q7EeA2e8M5Qhnw7RS34VdRIVhmt5y3dHqc2o=
[rockylinux@workstation ~]$ vim ~/.ssh/known_hosts
[rockylinux@workstation ~]$ cat ~/.ssh/known_hosts
[rockylinux@workstation ~]$ ssh rockylinux@127.0.0.1
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:jtNpnVUOQFBlVo5shPfHdnogsQq/LtxbFLmgaeJJDjI.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

有關於SSH連線之間做了什麼事情,由下列的示意圖所示:

SSH architecture

另外,SSH有分版本,分別為1與2,目前都是使用第2版,而第2版就會有四種host key在作業系統安裝的時候給產生出來,主要有RSA、DSA、ECDSA與ECD25519等幾種,相關的所自動產生的所有Host公鑰可以在/etc/ssh目錄底下找到,相關的指令執行所輸出的訊息如下:

[rockylinux@workstation ~]$ ls /etc/ssh/
moduli      ssh_config.d  ssh_host_ecdsa_key      ssh_host_ed25519_key      ssh_host_rsa_key
ssh_config  sshd_config   ssh_host_ecdsa_key.pub  ssh_host_ed25519_key.pub  ssh_host_rsa_key.pub

課後練習

  • 請將workstation虛擬機器啟動,並登入進rockylinux之使用者並開啟終端機。
  • 嘗試使用ssh rockylinux@localhost指令登入並驗證fingerprint。
  • 成功遠端登入進去之後,退出遠端的Shell。
  • 將rockylinux之家目錄下的.ssh/known_hosts檔案刪除,並再執行一次ssh rockylinux@localhost觀察輸出的訊息,並試著修復。
  • 上述的問題進行排除與修復之後,應可以成功連線並回到詢問fingerprint之訊息了。

上一篇
第11章:控制背景運行服務介紹
下一篇
第12章:SSH遠端連線設定與原理介紹(二)
系列文
Rocky Linux 8:系統管理與維運實戰23
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言